import ddapi from './ddapi'
import Vue from 'vue'

export class AuthService {
    //签名
    autograph() {
        let configUrl = "/api/auth/getJsConfig?url=http://192.168.3.186:8081/?corpid=ding60943bcc4a8f79ba35c2f4657eb6378f#/" + encodeURIComponent(document.URL);
        return new Promise(function (resolve) {
            Vue.http.get(configUrl).then(res => {
                // alert(JSON.stringify(res))
                let jsConfig = res.data;
                jsConfig.jsApiList = [
                    'runtime.info',
                    'biz.contact.choose',
                    'device.notification.confirm',
                    'device.notification.alert',
                    'device.notification.prompt',
                    'device.launcher.launchApp',
                    'biz.user.get',
                    "biz.util.uploadImage",
                    "biz.contact.complexPicker",
                    "biz.contact.choose",
                    "runtime.permission.requestOperateAuthCode",
                    "biz.contact.departmentsPicker",
                    "biz.util.chosen", "biz.util.uploadImageFromCamera"
                ]; // 必填，需要使用的jsapi列表，注意：不要dd。
                ddapi.config(jsConfig);

                localStorage.setItem("corpId", jsConfig.corpId);


                dd.ready(function () {
                    resolve();
                });
                ddapi.error().then(res => {
                    alert(JSON.stringify(res))
                })
            });
        })
    }

    //免登
    authLogin() {
        let t = this;
        return new Promise(function (resolve) {
            t.autograph().then(res => {
                ddapi.ready().then(() => {
                    let corpid = "ding60943bcc4a8f79ba35c2f4657eb6378f";
                    ddapi.requestAuthCode(corpid).then(code => {
                        //alert(code);
                        Vue.http.get('/api/auth/login?code=' + code).then(res => {
                            //免登钥匙
                            localStorage.setItem('authorization', res.data.token);
                            resolve()
                        }, err => {
                            alert(JSON.stringify(err))
                        })
                    })
                });
            })
        })
    }
}


let authservice = new AuthService()
export default authservice;